package com.zhuoyue.web.controller;

import com.zhuoyue.biz.core.api.service.impl.UserContentReadService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.dto.ControllerResult;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 内容管理系统
 *
 * @author wjming
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/content")
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppContextController {


    @GetMapping("/notice/dynamic")
    @CheckLogin
    public ControllerResult getContent(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        String userUrl = "http://121.40.241.191:8989/system/movement/listForApi?orgCode=" + user.getOrgCode();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Rep> forEntity = restTemplate.getForEntity(userUrl, Rep.class);
        Rep body = forEntity.getBody();

        return ControllerResult.toReturnDataSuccess("成功", body.getRows());
    }

    @PostMapping("/notice/dynamic/read")
    @CheckLogin
    public ControllerResult noticeRead(String contentId) {
        String userUrl = "http://121.40.241.191:8989/system/movement/addClickCount?id=" + contentId;
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.put(userUrl, Object.class);
        return ControllerResult.toReturnMessageSuccess("成功");
    }
}

@Data
class Rep {
    private long total;
    private List<?> rows;
    private int code;
    private String msg;
}

@Data
class SysMovementDTO {
    private Long id;
    /**
     * 类型, pictureOnly/pictureAndText(图片/图文)
     */
    private String dictValue;
    /**
     * 标题
     */
    private String title;
    /**
     * 内容, 若为图片动态时, 则是图片的链接
     */
    private String content;
    /**
     * 图标的地址(仅为图文类型时有效)
     */
    private String iconUrl;
    /**
     * 点击调转后的链接
     */
    private String redirectUrl;
    /**
     * 点击量
     */
    private Long clickCount;
    /**
     * 是否为轮播图(0是 1否)
     */
    private String swiper;


    private Long createTime;
}
